﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Timingrules
    {
        public int Add(Chain.Model.Timingrules model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Timingrules(");
            builder.Append("RulesName,RulesInterval,RulesUnitPrice,RulesExceedTime,RulesAddTime,RulesShopID,RulesUserID,RulesRemark)");
            builder.Append(" values (");
            builder.Append("@RulesName,@RulesInterval,@RulesUnitPrice,@RulesExceedTime,@RulesAddTime,@RulesShopID,@RulesUserID,@RulesRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RulesName", SqlDbType.NVarChar, 50), new SqlParameter("@RulesInterval", SqlDbType.Int, 4), new SqlParameter("@RulesUnitPrice", SqlDbType.Money, 8), new SqlParameter("@RulesExceedTime", SqlDbType.Int, 4), new SqlParameter("@RulesAddTime", SqlDbType.DateTime), new SqlParameter("@RulesShopID", SqlDbType.Int, 4), new SqlParameter("@RulesUserID", SqlDbType.Int, 4), new SqlParameter("@RulesRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.RulesName;
            cmdParms[1].Value = model.RulesInterval;
            cmdParms[2].Value = model.RulesUnitPrice;
            cmdParms[3].Value = model.RulesExceedTime;
            cmdParms[4].Value = model.RulesAddTime;
            cmdParms[5].Value = model.RulesShopID;
            cmdParms[6].Value = model.RulesUserID;
            cmdParms[7].Value = model.RulesRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int RulesID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Timingrules ");
            builder.Append(" where RulesID=@RulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RulesID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RulesID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string RulesIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Timingrules ");
            builder.Append(" where RulesID in (" + RulesIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int RulesID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Timingrules");
            builder.Append(" where RulesID=@RulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RulesID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RulesID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM Timingrules ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" * FROM Timingrules ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.RulesID desc");
            }
            builder.Append(")AS Row, T.*  from Timingrules T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "Timingrules,SysUser";
            string[] column = new string[] { "Timingrules.*,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "RulesAddTime", "RulesID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.Timingrules GetModel(int RulesID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from Timingrules ");
            builder.Append(" where RulesID=@RulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RulesID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RulesID;
            Chain.Model.Timingrules timingrules = new Chain.Model.Timingrules();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["RulesID"] != null) && (set.Tables[0].Rows[0]["RulesID"].ToString() != ""))
            {
                timingrules.RulesID = int.Parse(set.Tables[0].Rows[0]["RulesID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesName"] != null) && (set.Tables[0].Rows[0]["RulesName"].ToString() != ""))
            {
                timingrules.RulesName = set.Tables[0].Rows[0]["RulesName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RulesInterval"] != null) && (set.Tables[0].Rows[0]["RulesInterval"].ToString() != ""))
            {
                timingrules.RulesInterval = int.Parse(set.Tables[0].Rows[0]["RulesInterval"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesUnitPrice"] != null) && (set.Tables[0].Rows[0]["RulesUnitPrice"].ToString() != ""))
            {
                timingrules.RulesUnitPrice = decimal.Parse(set.Tables[0].Rows[0]["RulesUnitPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesExceedTime"] != null) && (set.Tables[0].Rows[0]["RulesExceedTime"].ToString() != ""))
            {
                timingrules.RulesExceedTime = int.Parse(set.Tables[0].Rows[0]["RulesExceedTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesAddTime"] != null) && (set.Tables[0].Rows[0]["RulesAddTime"].ToString() != ""))
            {
                timingrules.RulesAddTime = DateTime.Parse(set.Tables[0].Rows[0]["RulesAddTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesShopID"] != null) && (set.Tables[0].Rows[0]["RulesShopID"].ToString() != ""))
            {
                timingrules.RulesShopID = int.Parse(set.Tables[0].Rows[0]["RulesShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesUserID"] != null) && (set.Tables[0].Rows[0]["RulesUserID"].ToString() != ""))
            {
                timingrules.RulesUserID = int.Parse(set.Tables[0].Rows[0]["RulesUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RulesRemark"] != null) && (set.Tables[0].Rows[0]["RulesRemark"].ToString() != ""))
            {
                timingrules.RulesRemark = set.Tables[0].Rows[0]["RulesRemark"].ToString();
            }
            return timingrules;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Timingrules ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.Timingrules model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Timingrules set ");
            builder.Append("RulesName=@RulesName,");
            builder.Append("RulesInterval=@RulesInterval,");
            builder.Append("RulesUnitPrice=@RulesUnitPrice,");
            builder.Append("RulesExceedTime=@RulesExceedTime,");
            builder.Append("RulesAddTime=@RulesAddTime,");
            builder.Append("RulesShopID=@RulesShopID,");
            builder.Append("RulesUserID=@RulesUserID,");
            builder.Append("RulesRemark=@RulesRemark");
            builder.Append(" where RulesID=@RulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RulesName", SqlDbType.NVarChar, 50), new SqlParameter("@RulesInterval", SqlDbType.Int, 4), new SqlParameter("@RulesUnitPrice", SqlDbType.Money, 8), new SqlParameter("@RulesExceedTime", SqlDbType.Int, 4), new SqlParameter("@RulesAddTime", SqlDbType.DateTime), new SqlParameter("@RulesShopID", SqlDbType.Int, 4), new SqlParameter("@RulesUserID", SqlDbType.Int, 4), new SqlParameter("@RulesID", SqlDbType.Int, 4), new SqlParameter("@RulesRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.RulesName;
            cmdParms[1].Value = model.RulesInterval;
            cmdParms[2].Value = model.RulesUnitPrice;
            cmdParms[3].Value = model.RulesExceedTime;
            cmdParms[4].Value = model.RulesAddTime;
            cmdParms[5].Value = model.RulesShopID;
            cmdParms[6].Value = model.RulesUserID;
            cmdParms[7].Value = model.RulesID;
            cmdParms[8].Value = model.RulesRemark;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

